package com.hf.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hf.entity.Sign;
import com.hf.entity.SignParam;
import com.hf.entity.Users;
import com.hf.service.ISignService;
import com.hf.service.IUsersService;
import com.hf.util.Result;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author eight
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/sign")
public class SignController {
	private static final String Users = null;
	@Autowired
	ISignService iSignService;
	@Autowired
	IUsersService iUsersService;
	// 设置签到/签退间
	Date date = new Date();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 分页查询签到信息
	 * 
	 * @param s
	 * @return
	 */
	@RequestMapping("/getQianDaoList")
	public Object getList(SignParam s) {
		IPage<Sign> p = iSignService.getList(s);
		List<Sign> list = p.getRecords();
		int num = (int) p.getTotal();
		return new Result(0, "查詢成功", list, num);

	}

	/**
	 * 插入签到信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/addQianDao")
	public Object addQianDao(Sign s, Integer userId) {
		System.out.println("userId的值是：" + userId);
		Users user = iUsersService.getUserByUserId(userId);

		if (user.getIsQiandao().equals("已签到")) {
			return new Result(1, "已经签到，不能重复签到");
		} else {
			// 签到表插入数据
			s.setIsSignon("已签到");
			s.setSignOnTime(sdf.format(date));
			s.setUserId(userId);
			boolean res = iSignService.save(s);
			if (res) {
				// 用户表：修改敲到状态
				boolean updateIsQianDao = iUsersService.updateIsQianDao(userId);

				return new Result(0, "签到成功");
			}
			return new Result(1, "签到失败");
		}

	}

	/**
	 * 签退之后修改信息
	 * 
	 * @param s
	 * @return
	 */
	@RequestMapping("/uppdateQianTui")
	public Object uppdateQianTui(Sign s, Integer userId) {
		System.out.println("id参数：" + userId);
		Users user = iUsersService.getUserByUserId(userId);
		if (!user.getIsQiandao().equals("已签到")) {
			return new Result(1, "你还没有签到，不能签退");
		} else if (user.getIsQiandao().equals("已签退")) {
			return new Result(1, "已经签退，不能重复签退");
		} else {
			boolean res = iSignService.updateQianTui(userId, sdf.format(date));
			if (res) {
				boolean updateIsQianTui = iUsersService.updateIsQianTui(userId);
				return new Result(0, "签退成功");
			}
			return new Result(1, "签退失败");
		}

	}

}
